package com.gscitysfy.cus.drain.modules.pipe.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkFlaw;
import com.gscitysfy.cus.drain.modules.pipe.mapper.DrainPipeNetworkFlawMapper;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeNetworkFlawService;
import com.gscitysfy.cus.drain.modules.pipe.vo.DrainPipeNetworkFlawVo;
import com.gscitysfy.cus.drain.utils.DateUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-11-15 10:50:56
 */
@Service
public class DrainPipeNetworkFlawServiceImpl extends ServiceImpl<DrainPipeNetworkFlawMapper, DrainPipeNetworkFlaw> implements IDrainPipeNetworkFlawService {

    @Resource
    private DrainPipeNetworkFlawMapper drainPipeNetworkFlawMapper;


    /**
     * 分页获取检测缺陷列表
     * @param drainPipeNetworkFlawVo
     * @return
     */
    @Override
    public Page<DrainPipeNetworkFlaw> getFlawListByCondition(Page<DrainPipeNetworkFlaw> page, DrainPipeNetworkFlawVo drainPipeNetworkFlawVo) {
        Page<DrainPipeNetworkFlaw> flawListByCondition = drainPipeNetworkFlawMapper.getFlawListByCondition(page,drainPipeNetworkFlawVo);
        return flawListByCondition;
    }


    /**
     * 上报缺陷
     * @param drainPipeNetworkFlaw
     * @return
     */
    @Override
    public Object saveFlaw(DrainPipeNetworkFlaw drainPipeNetworkFlaw) {
        drainPipeNetworkFlaw.setCmKid(null);
        Date date = new Date();
        drainPipeNetworkFlaw.setCmFlawTime(date);
        drainPipeNetworkFlaw.setCmPipeLine(drainPipeNetworkFlaw.getCmPipeNo());
        drainPipeNetworkFlaw.setCmFlawNo(this.createFlawNo());
        return this.save(drainPipeNetworkFlaw);
    }


    /**
     * Gis列表查询
     * @param drainPipeNetworkFlawVo
     * @return
     */
    @Override
    public List<DrainPipeNetworkFlaw> getFlaw(DrainPipeNetworkFlawVo drainPipeNetworkFlawVo) {
        List<DrainPipeNetworkFlaw> flawByCondition = drainPipeNetworkFlawMapper.getFlaw(drainPipeNetworkFlawVo);
        return flawByCondition;
    }


    /**
     * 生成缺陷编号
     * @return
     */
    private String createFlawNo(){
        String nextNo ="QX" +DateUtil.formatDate(new Date(), DateUtil.yyyy_MM_dd_HH_mm);
        LambdaQueryWrapper<DrainPipeNetworkFlaw> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(DrainPipeNetworkFlaw::getCmFlawNo, nextNo).orderByDesc(DrainPipeNetworkFlaw::getCmAddtime).last("limit 1");
        DrainPipeNetworkFlaw flaw = this.getOne(queryWrapper);
        if (!Objects.isNull(flaw)){
            String preNo = flaw.getCmFlawNo();
            String time = preNo.substring(preNo.length() - 3);
            String num = preNo.substring(0, preNo.length() - 3);
            DecimalFormat format = new DecimalFormat("00");
            nextNo = num + format.format(Integer.parseInt(time) + 1);
        }else{
            nextNo = nextNo + "01";
        }
        return nextNo;
    }
}
